Passed
Push — develop ( bfb867...215336 )
by Endre
03:51
created

Router   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 37
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 6

5 Functions

Rating   Name   Duplication   Size   Complexity  
A attachTo 0 3 1
A changePage 0 10 2
A onHistoryChange 0 4 1
A updatePage 0 3 1
A initialize 0 4 1
1
import {IObserver} from '../Observer/Observer';
2
3
export interface IPageData {
4
  name: string,
5
  url: string,
6
  depth: number
7
}
8
9
export default class Router {
10
  currentPage: IObserver<IPageData>;
11
  history: History;
12
13
  constructor(pageObserver: IObserver<IPageData>, history: History) {
14 5
    this.currentPage = pageObserver;
15 5
    this.history = history;
16
  }
17
18
  attachTo(window: Window) {
19 3
    window.addEventListener('popstate', this.onHistoryChange.bind(this));
20
  }
21
22
  initialize(firstPage: IPageData): void {
23 1
    this.history.replaceState(firstPage, firstPage.name, firstPage.url);
24 1
    this.updatePage(firstPage);
25
  }
26
27
  changePage(newPage: IPageData): void {
28 2
    const currentPage = this.currentPage.value;
29 2
    if (currentPage.name == newPage.name) {
30 1
      return;
31
    }
32
33 1
    this.history.pushState(newPage, newPage.name, newPage.url);
34
35 1
    this.updatePage(newPage);
36
  }
37
38
  updatePage(page: IPageData): void {
39 3
    this.currentPage.value = page;
40
  }
41
42
  onHistoryChange(event: PopStateEvent) {
43 1
    const newPage: IPageData = event.state as IPageData;
44 1
    this.updatePage(newPage);
45
  }
46
}